#include "qt_radio_ui.h"
#include "login_ui.h"
#include "room.h"
#include <QtGui>
extern "C"{
#include "message/clientMessage.h"
}
extern struct roomInfo myRoomInfo;
extern struct userInfo myUserInfo;
extern struct roomInfo usersList[];
extern int usersNum;
extern void (*getChatFunc)(char*);
extern void (*getUsersFunc)(void);
MainDialog *currChats;
void getChat_global(char* text){
	if(currChats!=NULL){
		currChats->rChat(text);
	}
}
void getUser_global(void){
	if(currChats!=NULL){
		currChats->getUsers();
	}
}

MainDialog::MainDialog()
{
    int cnt=0;
    createGridGroupBox();
    createMusicGroupBox();
    createIdGroupBox();

    okButton = new QPushButton(tr("OK"));
    cancelButton = new QPushButton(tr("Cancel"));
    okButton->setDefault(true);

    connect(buttons[0], SIGNAL(clicked()), this, SLOT(play()));
    connect(enterButton, SIGNAL(clicked()), this, SLOT(ok()));
    connect(buttons[1], SIGNAL(clicked()), this,SLOT(add()));
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(okButton);
    buttonLayout->addWidget(cancelButton);
    
    QVBoxLayout *rightLayout =new QVBoxLayout;
    rightLayout->addWidget(musicGroupBox);
    rightLayout->addWidget(idGroupBox);

    QHBoxLayout *groundLayout = new QHBoxLayout;
    groundLayout->addWidget(gridGroupBox);
    

    
   
    
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(groundLayout);
    mainLayout->addLayout(rightLayout);


    curdir =new QDir("./musicbox");
    for(cnt = 0; cnt < curdir->entryList().count(); cnt++) {
    if(!(curdir->entryList().at(cnt)=="." | curdir->entryList().at(cnt)==".."))
	musiclistwidget->addItem(curdir->entryList().at(cnt));
    };    


   RoomDialog *roomdia=new RoomDialog(this);
   Dialog *login=new Dialog(roomdia);
   login->show();
 



   setLayout(mainLayout);
    resize(600,400);
    setWindowTitle(tr("Radio Star"));
	currChats=this;
   
	getChatFunc=getChat_global;
	getUsersFunc=getUser_global;

}
void MainDialog::createIdGroupBox(){

    idGroupBox = new QGroupBox(tr("Id"));
    QVBoxLayout *layout =new QVBoxLayout;  
    
    idlistwidget = new QListWidget(this);
   
    layout->addWidget(idlistwidget);
   
    idGroupBox->setLayout(layout);
}

void MainDialog::createMusicGroupBox()
{
    musicGroupBox = new QGroupBox(tr("Music"));
    QVBoxLayout *layout = new QVBoxLayout;
    buttons[1] = new QPushButton(tr("add"));
    buttons[0] = new QPushButton(tr("play"));
    musiclistwidget = new QListWidget(this);
    


    layout->addWidget(musiclistwidget);
    layout->addWidget(buttons[0]);
    layout->addWidget(buttons[1]);

    musicGroupBox->setLayout(layout);
}

void MainDialog::createGridGroupBox()
{
    gridGroupBox = new QGroupBox(tr("chatting"));
    QGridLayout *layout = new QGridLayout;

 
    lineEdits = new QLineEdit;
    layout->addWidget(lineEdits,5,0);
   
   enterButton = new QPushButton(tr("input"));
   layout->addWidget(enterButton,5,1);    
     
    smallEditor = new QTextEdit;
    layout->addWidget(smallEditor,0,0,4,2);
    gridGroupBox->setLayout(layout);
}
void MainDialog::ok(){
QString chatText=QString::fromUtf8(myUserInfo.name)+": "+lineEdits->text();
smallEditor->append(chatText);
QByteArray ba;
ba=chatText.toUtf8();
char chats[80];
strcpy(chats,ba.data());
printf("%s\n",chats);
sendChat(chats,myRoomInfo.roomID);
lineEdits->clear();
}
void MainDialog::play(){

//fileName = QFileDialog::getOpenFileName(this,tr("Open File"),"","ALL (*.wav)");

//QFile file(fileName);
//if(file.open(QFile::ReadOnly | QFile::Text)){}
}
void MainDialog::add(){


 QFileDialog *fileDIG=new QFileDialog(this);
// fileDIG->setFileMode(QFileDialog::AnyFile);
 QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"),".musicbox","ALL (*.wav)");

}
void MainDialog::rChat(const char* text)
{
	QString qtext=QString::fromUtf8(text);
	smallEditor->append(qtext);
}
void MainDialog::getUsers(void){
	idlistwidget->clear();
	int i;
	for(i=0;i<usersNum;i++){
		const QString name=QString::fromUtf8(usersList[i].name);
		idlistwidget->insertItem(i,name);
	}
}
